{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "655fc286",
   "metadata": {},
   "source": [
    "# 集合\n",
    "## 定义\n",
    "* 集合（set）是一个**无序**且**不重复**的数据组合\n",
    "### 类型\n",
    "* 可变集合：**set（）**\n",
    "* 不可变集合：**frozenset**\n",
    "### 作用\n",
    "* **去重** ：把一个列表变成集合，去掉重复的元素\n",
    "* **关系测试**：可以测试两组数据的关系（交集，差集，并集）等\n",
    "### 运算支持\n",
    "* union（联合）\n",
    "* intersection（交）\n",
    "* difference（差）\n",
    "* sysmmertric difference（对称差集）等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0767decc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a: {'4', '1', '2', '3'}\n"
     ]
    }
   ],
   "source": [
    "a={\"1\",\"2\",\"2\",\"3\",\"3\",\"4\"}\n",
    "print(\"a:\",a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "81ea9758",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b: {'3', '2', '1'}\n"
     ]
    }
   ],
   "source": [
    "b=set([\"1\",\"2\",\"2\",\"3\",\"3\"])\n",
    "print(\"b:\",b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d79c0cb0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "交集： {'3', '4', '2', '1'}\n"
     ]
    }
   ],
   "source": [
    "print(\"并集：\" , a | b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "87efe07a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "交集： {'1', '2', '3'}\n"
     ]
    }
   ],
   "source": [
    "print(\"交集：\" , a & b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "24c55c33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "差集： {'4'}\n"
     ]
    }
   ],
   "source": [
    "print(\"差集：\" , a - b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b6c5661b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "对称差： {'4'}\n"
     ]
    }
   ],
   "source": [
    "print(\"对称差：\" , a ^ b)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
